<?xml version="1.0" encoding="UTF-8"?>
<definitions id="definitions"
  xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
  xmlns:camunda="http://camunda.org/schema/1.0/bpmn"
  targetNamespace="Examples">

  <process id="process" isExecutable="true">

    <startEvent id="theStart" />

    <sequenceFlow sourceRef="theStart" targetRef="task1" />

    <userTask id="task1" />

    <sequenceFlow sourceRef="task1" targetRef="scriptTask" />

    <scriptTask id="scriptTask" camunda:expression="${true}" scriptFormat="groovy">
      <extensionElements>
        <camunda:inputOutput>

          <camunda:inputParameter name="userId">
            <camunda:script scriptFormat="groovy">
              services = execution.getProcessEngineServices();

              // fetch current authentication
              identityService = services.getIdentityService();
              return identityService.getCurrentAuthentication().getUserId();
            </camunda:script>
          </camunda:inputParameter>

          <camunda:inputParameter name="count">
            <camunda:script scriptFormat="groovy">
              services = execution.getProcessEngineServices();

              // execute command
              runtimeService = services.getRuntimeService();
              runtimeService.startProcessInstanceByKey('process');

              // execute query
              return runtimeService.createProcessInstanceQuery().count();
            </camunda:script>
          </camunda:inputParameter>

        </camunda:inputOutput>
      </extensionElements>

      <script>
        execution.getProcessInstance().setVariable('userId', execution.getVariable('userId'));
        execution.getProcessInstance().setVariable('count', execution.getVariable('count'));
      </script>
    </scriptTask>

    <sequenceFlow sourceRef="scriptTask" targetRef="task2" />

    <userTask id="task2" />

    <sequenceFlow sourceRef="task2" targetRef="theEnd" />

    <endEvent id="theEnd" />

  </process>

</definitions>

